package com.elitel.common.businessJdbc.oracle;

import com.elitel.common.businessJdbc.BusinessSelectPreparedStatement;

public class OracleSelectPreparedStatement extends BusinessSelectPreparedStatement{

    @Override
    public String getPrecompileSql(){
        StringBuilder sb = new StringBuilder();
        if(isPageable()){
            sb.append("SELECT * FROM (  SELECT a.*, ROWNUM RN  FROM (SELECT ")
                .append(getFieldSection())
                .append(" FROM ")
                .append(getTableSection())
                .append(" where 1=1 ")
                .append(containPlaceHolderCondition())
                .append(getOrderSection())
                .append(" ) a  ")
                .append(" ) WHERE RN > ")
                .append(getStartSize())
                .append(" and RN <= ")
                .append(getEndSize());
        }else{
            sb.append("select a.* from ( ")
                .append(sqlTemplate())
                .append(getOrderSection())
                .append(" ) a");
        }
        return sb.toString();
    }


    public long getEndSize(){
        return getPageNumber() * getPageSize();
    }

    public long getStartSize(){
        return (getPageNumber() - 1 ) * getPageSize();
    }
}
